package com.perform.controller;

import com.domain.entity.User;
import com.domain.po.UserPo;
import com.domain.vo.UserVo;
import com.perform.service.IUserService;
import com.utils.BeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户微服务Controller
 * @author c-fanqj
 * @date 2022/8/23
 * @apiNoteE
 */
@RestController
@RequestMapping("/mission")
public class MissionController {

    private static final Logger logger =
            LoggerFactory.getLogger(MissionController.class);

    @Resource
    private IUserService iUserService;

    // 测试服务是否正常
    /*@GetMapping("/mytest/{name}")
    public String mytest(@PathVariable String name) {
        System.out.println("传入的值为：" + name);
        return "传入的值为：" + name;
    }*/

    @GetMapping("/findUserById/{id}")
    public String selectUserDetail(@PathVariable Integer id) {
        if(id == null) {
            return "参数id不存在！";
        }
        //UserPo userPo = BeanUtil.objects2obj(userVo, UserPo.class);
        return iUserService.findUserById(id).toString();
    }

    @GetMapping("/selectUserDetail")
    public String selectUserDetail(UserVo userVo) {
        if(userVo.getId() == null) {
            return "参数不存在！";
        }
        UserPo userPo = BeanUtil.objects2obj(userVo, UserPo.class);
        return iUserService.selectUserDetail(userPo);
    }

    @GetMapping("/list")
    public List<User> findUserList(Integer num) {
        if(num == null) {
            num = 1;
        }
        for (int i = 0; i < num; i++) {
            iUserService.findAllUsers();
        }
        return this.iUserService.findAllUsers();
    }

    /**
     * 添加一个student,使用postMapping接收post请求
     * http://localhost:8310/simple/addUser?username=user11&age=11&balance=11
     * @return
     */
    @PostMapping("/addUser")
    public User addUser(@RequestParam(value = "userName", required=false) String userName,
                        @RequestParam(value = "passWord", required=false) String passWord,
                        @RequestParam(value = "realName", required=false) String realName){
        User user = new User();
        user.setUserName(userName);
        user.setPassWord(passWord);
        user.setRealName(realName);
        int result = iUserService.insertUser(user);
        if(result > 0){
            return user;
        }
        user.setUserName(null);
        user.setPassWord(null);
        user.setRealName(null);
        return user;
    }

}
